//
//  XRSelfAddressViewController.m
//  XClassRoom
//
//  Created by NeiQuan on 16/11/8.
//  Copyright © 2016年 Mr-yuwei. All rights reserved.
//

#import "XRSelfAddressViewController.h"
#import "XRSelfAddressCell.h"
#import "PlacemanagerViewController.h"
#import "AddressModel.h"
#import "PlacemanagerTowViewController.h"
@interface XRSelfAddressViewController ()
{
    UIButton         *_footButton;
    NSInteger        page;
    NSMutableArray   *addressArray;
    NSInteger        flag;
}
@end

@implementation XRSelfAddressViewController
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self loadData];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    page=1;
    addressArray=@[].mutableCopy;
}
-(void)addNavbarView
{
    [super addNavbarView];
    self.title=@"收货地址";
}
-(void)addOwnViews{
    
    [super addOwnViews];
    [self  addFootView];
   [self.tableView setFrame:CGRectMake(0, 0, kMainScreenWidth, kMainScreenHeight-kNavBarHeight-kNavStausHeight-45)];
    [self.tableView registerNib:[UINib nibWithNibName:@"XRSelfAddressCell" bundle:nil] forCellReuseIdentifier:@"addressCell"];
    self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor=xBackgroundColor;
    [self tablePullupHeaderRefresh:^{

        [self loadData];
    }];
    [self tablePullDownFootRefresh:^{
        
        [self loadData];
    }];
    [self.view showActivitylodingViewWithstring:@"努力加载中..."];
}
-(void)addFootView
{
    __weak typeof(self)weakself=self;
     _footButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [_footButton setFrame:CGRectMake(0, kMainScreenHeight-kNavBarHeight-kNavStausHeight-40, kMainScreenWidth,40 )];
    [_footButton setTitle:@"添加新地址" forState:UIControlStateNormal];
    [_footButton setBackgroundColor:kNavBarThemeColor];
    [_footButton setTitleColor:kWhiteColor forState:UIControlStateNormal];
    _footButton .titleLabel.font=[UIFont systemFontOfSize:14];
    [self.view addSubview:_footButton];
    [_footButton addTarget:weakself action:@selector(pushToAddVc) forControlEvents:UIControlEventTouchUpInside];
}
-(void)loadData
{
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.view removeActivitylodingView];
        [self tableViewEndRefresh];
    });
}
#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return addressArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XRSelfAddressCell *cell=[tableView dequeueReusableCellWithIdentifier:@"addressCell" forIndexPath:indexPath];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.model=addressArray[indexPath.row];
    cell.flagBtn.tag=indexPath.row+1;
    cell.compileBtn.tag=indexPath.row+1;
    cell.deleteBtn.tag=indexPath.row+1;
    ///修改默认地址
    [cell.flagBtn addTarget:self action:@selector(setToTheDefaultAddress:) forControlEvents:UIControlEventTouchUpInside];
    ///编辑地址
    [cell.compileBtn addTarget:self action:@selector(editTheShippingAddress:) forControlEvents:UIControlEventTouchUpInside];
    ///删除收货地址
    [cell.deleteBtn addTarget:self action:@selector(deleteTheShippingAddress:) forControlEvents:UIControlEventTouchUpInside];
    
    return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
    
    }
}
#pragma mark - UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    AddressModel *model=addressArray[indexPath.row];
    NSString *string=[model.addr stringByAppendingString:model.detail];
    CGFloat  height=[NSString sizeWithString:string font:[UIFont systemFontOfSize:13] constrainedToWidth:kMainScreenWidth-20]+95;
    return height<110?110:height;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
    AddressModel *model=addressArray[indexPath.row];
    if (_VcType==AddressViewControllerdefaultType)
    {
        PlacemanagerTowViewController *addVc=[[PlacemanagerTowViewController alloc] init];
        addVc.model=model;
        [self.navigationController pushViewController:addVc animated:YES];
    }else
    {
        if (_block)
        {
            _block(model);
        }
        [self.navigationController popViewControllerAnimated:YES];
    }
}
#pragma mark --跳转到添加地址界面
-(void)pushToAddVc
{
    PlacemanagerViewController   *addVc=[[PlacemanagerViewController alloc] init];
    [self.navigationController pushViewController:addVc animated:YES];
}
#pragma mark - 修改默认地址
-(void)setToTheDefaultAddress:(UIButton*)btn
{
    AddressModel *model=addressArray[btn.tag-1];
    if (model.flag==0) {
        flag=1;
    }else{
        flag=0;
    }
}
#pragma mark - 编辑地址
-(void)editTheShippingAddress:(UIButton*)btn
{
    AddressModel *model=addressArray[btn.tag-1];
    PlacemanagerTowViewController *addVc=[[PlacemanagerTowViewController alloc] init];
    addVc.model=model;
    [self.navigationController pushViewController:addVc animated:YES];
}
#pragma mark - 删除地址
-(void)deleteTheShippingAddress:(UIButton*)btn
{
    
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
@end
